package com.hualude.roadsign.service.impl;


import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.SignfaultEntity;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.SignfaultService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xu-jiaHui on 2017/2/28.
 */
@Service
public class SignfaultServiceImpl implements SignfaultService {
    @Resource
    private SignfaultRepository signfaultRepository;

    @Resource
    private RoadPoleRepository roadPoleRepository;

    @Resource
    private MaintenanceRepository mainTenanceRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private RoadSignRepository roadSignRepository;

    @Resource
    private OrgRepository orgRepository;

    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getSignfault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer gActorId, Integer userId, Integer orgId) throws Exception {
        Integer count = 0;
        List<SignfaultDto> dto4DTs = new ArrayList<SignfaultDto>();
        //主管审核人
        if (gActorId == 7) {
            count = signfaultRepository.getFaultAuditCount(dtRequestDto, actorId, userId, orgId);
            dto4DTs = signfaultRepository.getSignfault4Audit(dtRequestDto, actorId, userId, orgId);
        } else if (gActorId == 3) {
            count = signfaultRepository.getCount4DT(actorId, userId, orgId, dtRequestDto.getSearch());
            dto4DTs = signfaultRepository.getSignfault4DT(dtRequestDto, actorId, userId, orgId);
        }
        //获取每条上报人的姓名电话
        for (SignfaultDto dto : dto4DTs) {
            UserDto userDto = userRepository.getUserById(dto.getSignFaultPeopleId());
            dto.setSignFaultPeopleName(userDto.getUserName());
            dto.setSignFaultPeopleMobile(userDto.getUserMobile());
        }
        List<UserDto> userDtos = userRepository.getUserNameIdAndOrgId();
        boolean t1, t2, t3, t4;
        for (SignfaultDto signfaultDto : dto4DTs) {
            t1 = false;
            t2 = false;
            t3 = false;
            t4 = false;
            for (UserDto userDto : userDtos) {
                if (t1 && t2 && t3 && t4) break;
                else {
                    if (userDto.getUserId() == signfaultDto.getMainUserId()) {
                        signfaultDto.setMainCheckName(userDto.getUserName());
                        t1 = true;
                    }
                    if (userDto.getUserId() == signfaultDto.getCompanyUserId()) {
                        signfaultDto.setCompanyUserName(userDto.getUserName());
                        t2 = true;
                    }
                    if (signfaultDto.getMaintenancePeopleId() != null) {
                        if (userDto.getUserId() == signfaultDto.getMaintenancePeopleId()) {
                            signfaultDto.setMaintenancePeopleName(userDto.getUserName());
                            t3 = true;
                        }
                    }
                    if (userDto.getUserId() == signfaultDto.getSignFaultPeopleId()) {
                        signfaultDto.setSignFaultPeopleName(userDto.getUserName());
                        t4 = true;
                    }
                }
            }
        }
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    @Transactional
    public Integer deleteFaultReportByIds(String FaultReportIds) throws Exception {
        String[] FaultReportIdArray = FaultReportIds.split(",");
        int i = 0;
        for (i = 0; i < FaultReportIdArray.length; i++) {
            signfaultRepository.deleteSignFaultById(Integer.parseInt(FaultReportIdArray[i]));
        }
        return i;
    }

    @Transactional
    public Integer updateSignFaultById(Integer signFaultId, Integer signFaultLevel, Integer signFaultType, String signFaultPic,
                                       String signFaultReason, Double repairAmount) throws Exception {
        return signfaultRepository.updateSignFaultById(signFaultId, signFaultLevel, signFaultType, signFaultPic,
                signFaultReason, repairAmount);
    }

    public List<RoadpoleDto> getlongLatitude4Map(Integer roadSignId) throws Exception {
        return signfaultRepository.getlongLatitude4Map(roadSignId);
    }

    public List<SignfaultEntity> getSignfaultInfoByRoadSignId(Integer roadSignId) throws Exception {
        return signfaultRepository.getSignfaultInfoByRoadSignId(roadSignId);
    }


    @Transactional
    public Integer signFaultAudit(Integer isPass, String signFaultCheckBack, Integer signFaultId, Integer actorId, String signCheckTime, Integer maintenancePeopleId, Integer userId, final Integer roadSignId, String signFaultReason, Integer signFaultLevel, final Integer signFaultType, String signFaultPic, String signFaultTime, Double repairAmount) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        signfaultRepository.updateSignCheckTime(signCheckTime, signFaultId);
        if (actorId == 7)//主管单位审核
        {
            if (isPass == 1)//审核通过,插入maintenance表
            {
                SignfaultDto signfaultDto = new SignfaultDto();
                signfaultDto.setSignFaultId(signFaultId);
                signfaultDto.setSignFaultLevel(signFaultLevel);
                signfaultDto.setSignFaultPic(signFaultPic);
                signfaultDto.setSignFaultType(signFaultType);
                signfaultDto.setSignFaultReason(signFaultReason);
                signfaultDto.setSignFaultTime(dateNowStr);
                signfaultDto.setRoadSignId(roadSignId);
                signfaultDto.setSignFaultCheckBack(signFaultCheckBack);
                signfaultDto.setRepairAmount(repairAmount);
                mainTenanceRepository.save(signfaultDto.toBean()).getMaintenanceId();

                final String[] strSignFaultType = {"正常", "膜脱落", "膜淡化", "逆反射系数不达标", "卷边",
                        "转向", "非国际", "抱箍缺少", "净空高不达标", "设置不合理", "缺失", "建议增设",
                        "与交通组织矛盾", "内容有误", "标牌与标杆不配套", "非公指示牌"};
                final Integer orgId = roadSignRepository.getOrgIdByRoadSignId(roadSignId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "标牌 NO：" + roadSignId + " " + strSignFaultType[signFaultType];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),1);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }//更新signFault表的状态
            return signfaultRepository.signFaultAuditByMain(isPass, signFaultCheckBack, signFaultId, userId);
        } else return 0;
    }

    @Transactional
    public Integer signFaultAudit2Report(Integer isPass, String signFaultCheckBack, Integer signFaultId, Integer actorId, String signCheckTime, Integer maintenancePeopleId, Integer signFaultPeopleId, Integer userId) throws Exception {
        signfaultRepository.updateSignCheckTime(signCheckTime, signFaultId);
        if (isPass == 1) {
            final SignfaultEntity signfaultEntity = signfaultRepository.getSignfaultBySignFaultId(signFaultId);
            List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(signfaultEntity.getMainOrgId(), 7);
            final String msg = "标牌 NO：" + signfaultEntity.getRoadSignId() + " 待审核";
            strUserIds = "";
            for (UserDto userDto : userDtos) {
                strUserIds += userDto.getUserId().toString() + ",";
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!strUserIds.equals("")) {
                            strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                            PushUtil.sendPushToUser(strUserIds, msg);
                            PushHistoryDto pushHistoryDto = new PushHistoryDto(signfaultEntity.getMainOrgId(), 7, msg, formatter.format(new Date()),1);
                            pushHistoryRepository.save(pushHistoryDto.toBean());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else if (isPass == 2) {//直接通过 将signFault表上的内容添加到维护表
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateNowStr = sdf.format(d);
            SignfaultEntity signfaultEntity = signfaultRepository.getSignfaultBySignFaultId(signFaultId);
            SignfaultDto signfaultDto = new SignfaultDto();
            signfaultDto.setSignFaultId(signFaultId);
            signfaultDto.setSignFaultLevel(signfaultEntity.getSignFaultLevel());
            signfaultDto.setSignFaultPic(signfaultEntity.getSignFaultPic());
            signfaultDto.setSignFaultType(signfaultEntity.getSignFaultType());
            signfaultDto.setSignFaultReason(signfaultEntity.getSignFaultReason());
            signfaultDto.setSignFaultTime(dateNowStr);
            signfaultDto.setRoadSignId(signfaultEntity.getRoadSignId());
            signfaultDto.setSignFaultCheckBack(signFaultCheckBack);
            signfaultDto.setRepairAmount(signfaultEntity.getRepairAmount());
            mainTenanceRepository.save(signfaultDto.toBean()).getMaintenanceId();

            final String[] strSignFaultType = {"正常", "膜脱落", "膜淡化", "逆反射系数不达标", "卷边",
                    "转向", "非国际", "抱箍缺少", "净空高不达标", "设置不合理", "缺失", "建议增设",
                    "与交通组织矛盾", "内容有误", "标牌与标杆不配套", "非公指示牌"};
            final Integer orgId = roadSignRepository.getOrgIdByRoadSignId(signfaultEntity.getRoadSignId());
            List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
            final String msg = "标牌 NO：" + signfaultEntity.getRoadSignId() + " " + strSignFaultType[signfaultEntity.getSignFaultType()];
            strUserIds = "";
            for (UserDto userDto : userDtos) {
                strUserIds += userDto.getUserId().toString() + ",";
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!strUserIds.equals("")) {
                            strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                            PushUtil.sendPushToUser(strUserIds, msg);
                            PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),1);
                            pushHistoryRepository.save(pushHistoryDto.toBean());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return signfaultRepository.signFaultAudiByCompany(isPass, signFaultCheckBack, signFaultId, signFaultPeopleId, maintenancePeopleId);
    }

    @Transactional
    public Integer updateSignFault2fail(Integer signFaultId, Integer status, String signFaultCheckBack, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);

        if (signfaultRepository.judgeCheck(signFaultId) == 1) {//若企业负责人已审核,主管负责人审核
            return signfaultRepository.updateSignFault2failByMain(signFaultId, status, signFaultCheckBack, dateNowStr, userId);
        } else
            return signfaultRepository.updateSignFault2failByCompany(signFaultId, status, signFaultCheckBack, dateNowStr, userId);
    }

    @Transactional
    public Integer updateSignFault2success(Integer signFaultId, Integer status, String signFaultCheckBack, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        if (signfaultRepository.judgeCheck(signFaultId) == 1) {//若企业负责人已审核,主管负责人审核
            signfaultRepository.updateSignFault2successByMain(signFaultId, status, signFaultCheckBack, dateNowStr, userId);
            //数据插入到maintenance表
            List<SignfaultDto> signfaultDtos = signfaultRepository.getSignFaultINfo4addBySignFaultId(signFaultId);
            for (SignfaultDto dto : signfaultDtos) {
                dto.setCompanyCheck(1);
                dto.setMainCheck(1);
                dto.setSignFaultId(signFaultId);
                dto.setSignFaultTime(dateNowStr);
                mainTenanceRepository.save(dto.toBean()).getMaintenanceId();
            }

            final String[] strSignFaultType = {"正常", "膜脱落", "膜淡化", "逆反射系数不达标", "卷边",
                    "转向", "非国际", "抱箍缺少", "净空高不达标", "设置不合理", "缺失", "建议增设",
                    "与交通组织矛盾", "内容有误", "标牌与标杆不配套", "非公指示牌"};
            final SignfaultDto signfaultDto = signfaultDtos.get(0);
            final Integer orgId = roadSignRepository.getOrgIdByRoadSignId(signfaultDto.getRoadSignId());
            List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
            final String msg = "标牌 NO：" + signfaultDto.getRoadSignId() + " " +
                    strSignFaultType[signfaultDto.getSignFaultType()];
            strUserIds = "";
            for (UserDto userDto : userDtos) {
                strUserIds += userDto.getUserId().toString() + ",";
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!strUserIds.equals("")) {
                            strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                            PushUtil.sendPushToUser(strUserIds, msg);
                            PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),1);
                            pushHistoryRepository.save(pushHistoryDto.toBean());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return 1;
        } else {
            final SignfaultEntity signfaultEntity = signfaultRepository.getSignfaultBySignFaultId(signFaultId);
            List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(signfaultEntity.getMainOrgId(), 7);
            final String msg = "标牌 NO：" + signfaultEntity.getRoadSignId() + " 待审核";
            strUserIds = "";
            for (UserDto userDto : userDtos) {
                strUserIds += userDto.getUserId().toString() + ",";
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (!strUserIds.equals("")) {
                            strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                            PushUtil.sendPushToUser(strUserIds, msg);
                            PushHistoryDto pushHistoryDto = new PushHistoryDto(signfaultEntity.getMainOrgId(), 7, msg, formatter.format(new Date()),1);
                            pushHistoryRepository.save(pushHistoryDto.toBean());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return signfaultRepository.updateSignFault2successByCompany(signFaultId, status, signFaultCheckBack, dateNowStr, userId);
        }
    }


    public List<SignFaultReportDto> getFaultList(Integer orgId, String actorIds, Integer faultStatus, Integer count,
                                                 Integer size) throws Exception {
        List<SignFaultReportDto> result = null;
        //未审核
        if (faultStatus == 1) {
            result = signfaultRepository.getUnauditedFault(orgId, actorIds, count, size);
        }
        //未通过
        else if (faultStatus == 2) {
            result = signfaultRepository.getUnpassFault(orgId, actorIds, count, size);
        }
        //已通过
        else if (faultStatus == 3) {
            result = signfaultRepository.getPassFault(orgId, actorIds, count, size);
        }
        return result;
    }


    @Transactional
    public Integer reportSignFault(final Integer roadSignId, Integer signFaultPeopleId, Integer signFaultLevel, Integer signFaultType,
                                   String signFaultPic, String signFaultReason, Double repairAmount, final Integer orgId, Integer mainOrgId) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if (repairAmount == null)
            repairAmount = 0.0;
        SignFaultReportDto insertDto = new SignFaultReportDto();
        insertDto.setRoadSignId(roadSignId);
        insertDto.setOrgId(orgId);
        insertDto.setMainOrgId(mainOrgId);
        insertDto.setSignFaultTime(df.format(new Date()));
        insertDto.setSignFaultPeopleId(signFaultPeopleId);
        insertDto.setSignFaultLevel(signFaultLevel);
        insertDto.setSignFaultPic(signFaultPic);
        insertDto.setSignFaultType(signFaultType);
        insertDto.setSignFaultReason(signFaultReason);
        insertDto.setRepairAmount(repairAmount);

        List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 3);
        final String msg = "标牌 NO：" + roadSignId + " 待审核";
        strUserIds = "";
        for (UserDto userDto : userDtos) {
            strUserIds += userDto.getUserId().toString() + ",";
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (!strUserIds.equals("")) {
                        strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                        PushUtil.sendPushToUser(strUserIds, msg);
                        PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 3, msg, formatter.format(new Date()),1);
                        pushHistoryRepository.save(pushHistoryDto.toBean());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        roadPoleRepository.updateRoadPoleSecurityBySignId(roadSignId);
        return signfaultRepository.save(insertDto.toBean()).getSignFaultId();
    }

    public DTResponseDto getCurrentFault4DT(DTRequestDto dtRequestDto, Integer actorId, String maintenancePeopleName) throws Exception {
        List<SignfaultDto> signfaultDtos = signfaultRepository.getCurrentFault4DT(dtRequestDto, actorId, maintenancePeopleName);
        List<UserDto> userDtos = userRepository.getUserNameIdAndOrgId();
        boolean t1, t2, t3;
        for (SignfaultDto signfaultDto : signfaultDtos) {
            t1 = false;
            t2 = false;
            t3 = false;
            for (UserDto userDto : userDtos) {
                if (t1 && t2 && t3) break;
                else {
                    if (userDto.getUserId() == signfaultDto.getCompanyUserId()) {
                        signfaultDto.setCompanyUserName(userDto.getUserName());
                        t1 = true;
                    }
                    if (userDto.getUserId() == signfaultDto.getMainUserId()) {
                        signfaultDto.setMainCheckName(userDto.getUserName());
                        t2 = true;
                    }
                    if (userDto.getUserId() == signfaultDto.getSignFaultPeopleId()) {
                        signfaultDto.setSignFaultPeopleName(userDto.getUserName());
                        t3 = true;
                    }
                }

            }
        }
        int count = signfaultRepository.getCurrentFaultCount4DT(dtRequestDto, actorId, maintenancePeopleName);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, signfaultDtos);
        return dtResponseDto;
    }

    public List<UserOrgDto4Notice> getOrgAndUserByUserId(Integer signFaultPeopleId) throws Exception {
        Integer userOrg = userRepository.getUserOrg4Notice(signFaultPeopleId);
        List<UserOrgDto4Notice> userOrgDtos = signfaultRepository.getUserInfo2ReportFaultByOrg(userOrg);
        return userOrgDtos;
    }

    public DTResponseDto getRejectedFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception {
        List<SignfaultDto> signfaultDtos = signfaultRepository.getRejectedFault4DT(dtRequestDto, actorId, userId);
        Integer count = signfaultRepository.getRejectedFaultCount4DT(dtRequestDto, actorId, userId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, signfaultDtos);
        return dtResponseDto;
    }

    @Transactional
    public Integer updateSignFaultCheckById(Integer isPass, Integer signFaultId, Integer actorId, Integer companyCheck, Integer mainCheck, String signCheckTime) throws Exception {
        if (actorId == 3)
            return signfaultRepository.updateSignFaultCompanyCheckById(isPass, signFaultId, signCheckTime);
        else if (actorId == 7)
            return signfaultRepository.updateSignFaultMainCheckById(isPass, signFaultId, signCheckTime);
        else if (actorId == 1) {
            if (mainCheck == -1)
                return signfaultRepository.updateSignFaultMainCheckById(isPass, signFaultId, signCheckTime);
            if (companyCheck == -1)
                return signfaultRepository.updateSignFaultCompanyCheckById(isPass, signFaultId, signCheckTime);
        }
        return 0;
    }

    public Integer getSignFaultUnAuditCount(Integer actorId, Integer gActorId, Integer userId, Integer orgId) throws Exception {
        Integer count = 0;
        if (gActorId == 7) {
            count = signfaultRepository.getFaultUnAuditCount(actorId, userId);
        } else if (gActorId == 3) {
            count = signfaultRepository.getUnCount(actorId, userId, orgId);
        }
        return count;
    }

    public RoadsignDto getSignInfoBySignId(Integer roadSignId) throws Exception {
        RoadsignDto roadsignDto = roadSignRepository.getSignInfoBySignId(roadSignId);
        String orgName = orgRepository.getMainOrgName(roadsignDto.getOrgId());
        String mainOrgName = orgRepository.getMainOrgName(roadsignDto.getMainOrgId());
        roadsignDto.setOrgName(orgName);
        roadsignDto.setMainOrgName(mainOrgName);
        return roadsignDto;
    }

    public Integer getSignFaultCount(Integer orgId, Integer actorId, Integer userId) throws Exception {
        DTRequestDto dtRequestDto = new DTRequestDto(0, 0, 0, "");
        return signfaultRepository.getFaultAuditCount(dtRequestDto, actorId, userId, orgId);
    }

    public List<OrgDto> getOrg4Select(Integer actorId, Integer orgId) throws Exception {
        return orgRepository.getOrg4Select(actorId, orgId);
    }

    @Transactional
    public Integer updateRoadSignAndPoleSecurity(Integer roadSignId) throws Exception {
        roadSignRepository.updateRoadSignSecurity(roadSignId);
        Integer roadPoleId = roadSignRepository.getRoadPoleIdByRoadSignId(roadSignId);
        return roadPoleRepository.updateRoadPoleSecurity(roadPoleId);
    }

    public String getCheckBackBySignFaultId(Integer roadSignId) throws Exception {
        return signfaultRepository.getCheckBackBySignFaultId(roadSignId);
    }


    public List<SignFaultReportDto> getSignFaultInfOByUserIdStatus(Integer userId,
                                                                   String actorIds,
                                                                   Integer count,
                                                                   Integer size,
                                                                   Integer status) throws Exception {
        List<SignFaultReportDto> result = null;
        //status:0获取待审核,1已通过,2未通过
        if (status == 0) {
            result = signfaultRepository.getUnauditedFaultByUserId(userId, actorIds, count, size);
        } else if (status == 1) {
            result = signfaultRepository.getPassFaultByuserId(userId, actorIds, count, size);
        } else if (status == 2) {
            result = signfaultRepository.getUnpassFaultByuserId(userId, actorIds, count, size);
        }
        return result;
    }


    public Integer getSignFaultStatus(Integer signFaultId, Integer roadSignId) throws Exception {
        Integer mainCheck;
        Integer companyCheck;
        Integer status = null;//进度状态
        Integer newSignFaultId = 0;
        Integer maintenanceStatus = 0;
        List<SignfaultDto> signFaultCheckDto = signfaultRepository.getSignFaultCheck(signFaultId);
        List<Integer> signFaultIdDto = signfaultRepository.getNewSignFaultId(roadSignId);
        List<Integer> maintenanceDto = mainTenanceRepository.getMaintenanceStatus(signFaultId);
        companyCheck = signFaultCheckDto.get(0).getCompanyCheck();
        mainCheck = signFaultCheckDto.get(0).getMainCheck();
        if (companyCheck == 0) {
            status = 0;//待内部审核
        } else if (companyCheck == -1) {
            status = -1;//内部拒绝
        } else if (companyCheck == 1 && mainCheck == 0) {
            status = 2;//待主管审核
        } else if (companyCheck == 1 && mainCheck == -1) {
            status = -2;//主管拒绝
        } else if ((companyCheck == 1 && mainCheck == 1) || companyCheck == 2) {
            newSignFaultId = signFaultIdDto.get(0);
            maintenanceStatus = maintenanceDto.get(0);
            //判断是否为最新的一条故障信息
            if (companyCheck == 1) {
                if (signFaultId.equals(newSignFaultId)) {
                    //最新一条，到维修状态
                    if (maintenanceStatus == 0) {
                        status = 7;//待维修
                    } else if (maintenanceStatus == 1) {
                        status = 8;//维修中
                    } else if (maintenanceStatus == 2) {
                        status = 3;//故障完成
                    }
                } else {
                    status = 3;//故障完成
                }
            } else if (companyCheck == 2) {
                if (maintenanceStatus == 0) {
                    status = 9;//待维修  直通
                } else if (maintenanceStatus == 1) {
                    status = 10;//维修中 直通
                } else if (maintenanceStatus == 2) {
                    status = 11;//故障完成 直通
                }
            }
        }
        return status;
    }

}
